package com.ant.common.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

/**
 * @author LB
 */
public class DoubleUtil {

    private static final long serialVersionUID = -3345205828566485102L;
    private static final Integer DEF_DIV_SCALE = 2;

    public static Double add(Double value1, Double value2) {
        if (Objects.isNull(value1)) {
            value1 = 0.00d;
        }

        if (Objects.isNull(value2)) {
            value2 = 0.00d;
        }

        BigDecimal b1 = new BigDecimal(Double.toString(value1));
        BigDecimal b2 = new BigDecimal(Double.toString(value2));
        return b1.add(b2).doubleValue();
    }

    public static double sub(Double value1, Double value2) {
        if (Objects.isNull(value1)) {
            value1 = 0.00d;
        }

        if (Objects.isNull(value2)) {
            value2 = 0.00d;
        }

        BigDecimal b1 = new BigDecimal(Double.toString(value1));
        BigDecimal b2 = new BigDecimal(Double.toString(value2));
        return b1.subtract(b2).doubleValue();
    }

    public static Double mul(Double value1, Double value2) {
        if (Objects.isNull(value1)) {
            value1 = 0.00d;
        }

        if (Objects.isNull(value2)) {
            value2 = 0.00d;
        }

        BigDecimal b1 = new BigDecimal(Double.toString(value1));
        BigDecimal b2 = new BigDecimal(Double.toString(value2));
        return b1.multiply(b2).doubleValue();
    }

    public static Double mul3(Double value1, Double value2, Double value3) {
        return mul(mul(value1, value2), value3);
    }

    public static Double divide(Double dividend, Double divisor) {
        return divide(dividend, divisor, DEF_DIV_SCALE);
    }

    public static Double divide2(Double dividend, Double divisor) {
        if (Objects.isNull(dividend) || divisor == 0.00) {
            return 0.00d;
        }
        return divide(dividend, divisor, 2);
    }

    public static Double divide4(Double dividend, Double divisor) {
        return divide(dividend, divisor, 4);
    }

    public static Double divide(Double dividend, Double divisor, Integer scale) {
        if (Objects.isNull(dividend)) {
            dividend = 0.00d;
        }

        if (Objects.isNull(divisor)) {
            divisor = 0.00d;
        }

        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(Double.toString(dividend));
        BigDecimal b2 = new BigDecimal(Double.toString(divisor));
        return b1.divide(b2, scale, RoundingMode.HALF_UP).doubleValue();
    }

    public static Double round(Double value, int scale) {
        if (Objects.isNull(value)) {
            value = 0.00d;
        }

        if (scale < 0) {
            scale = 2;
        }

        BigDecimal b = new BigDecimal(Double.toString(value));
        BigDecimal one = new BigDecimal("1");
        return b.divide(one, scale, RoundingMode.HALF_UP).doubleValue();
    }

    public static int trans2Int(double dVal) {
        return new Double(dVal).intValue();
    }

    public static Double round(Double value) {
        return round(value, 2);
    }
}
